#include<bits/stdc++.h>

using namespace std;

#define ll long long

const ll N=1e5+5;

ll T;
ll n,m;
ll aa[N],c[N],a[N];
ll k;
string s[25];
ll x[5];

ll ans;
bool A=1;
vector<ll> wn;
ll ck[N];

int main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);

    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);

    cin>>n>>m;
    for(ll i=1;i<=n;i++)
    {
        cin>>aa[i];
    }
    for(ll i=1;i<=m;i++)
    {
        cin>>c[i];
        ll n1=c[i];
        while(n1)
        {
            ck[i]++;
            n1/=2;
        }
        if((1<<(ck[i]-1)==c[i]))
        {
            ck[i]--;
        }
        else
        {
            A=0;
        }
    }

    ll n1=n;
    while(n1)
    {
        k++;
        n1/=2;
    }
    if(1<<(k-1)==n)
    {
        k--;
    }
    for(ll i=1;i<=k;i++)
    {
        cin>>s[i];
    }

    cin>>T;
    while(T--)
    {
        wn.clear();
        ans=0;
        for(ll i=0;i<4;i++)
        {
            cin>>x[i];
        }
        for(ll i=1;i<=n;i++)
        {
            a[i]=aa[i]^x[(i-1)%4];
        }
        vector<ll> tmp;
        tmp.clear();
        for(ll t=1;t<=m;t++)
        {
            wn.clear();
            for(ll i=1;i<=c[t];i++)
            {
                wn.push_back(i);
            }
            for(ll i=1;i<=ck[t];i++)
            {
                for(ll j=0;j<min((ll)s[i].size(),(ll)1<<(ck[t]-i+1));j++)
                {
                    if(s[i][j]==0)
                    {
                        if(a[wn[j*2]]<i)
                        {
                            tmp.push_back(wn[j*2+1]);
                        }
                        else
                        {
                            tmp.push_back(wn[j*2]);
                        }
                    }
                    else
                    {
                        if(a[wn[j*2+1]]<i)
                        {
                            tmp.push_back(wn[j*2]);
                        }
                        else
                        {
                            tmp.push_back(wn[j*2+1]);
                        }
                    }
                }
                wn=tmp;
                tmp.clear();
            }
            ans^=(t*wn[0]);
        }
        cout<<ans<<"\n";
    }

    return 0;
}
